package com.grace.controller;

import com.google.common.collect.Maps;
import com.grace.beans.PageQuery;
import com.grace.beans.PageResult;
import com.grace.common.JsonData;
import com.grace.dao.SysRoleUserMapper;
import com.grace.model.SysAcl;
import com.grace.model.SysRole;
import com.grace.model.SysUser;
import com.grace.param.AclParam;
import com.grace.service.SysAclService;
import com.grace.service.SysRoleAclService;
import com.grace.service.SysRoleService;
import com.grace.service.SysRoleUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 权限点控制器
 */
@Controller
@RequestMapping("/sys/acl")
@Slf4j
public class SysAclController {

    @Autowired
    private SysAclService sysAclService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleUserService sysRoleUserService;

    /**
     * 新增操作
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData save(AclParam param) {
        sysAclService.save(param);
        return JsonData.success();
    }

    /**
     * 更新操作
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData update(AclParam param) {
        sysAclService.update(param);
        return JsonData.success();
    }

    /**
     * 分页的方法
     */
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData page(@RequestParam("aclModuleId") int aclModuleId, PageQuery pageQuery) {
        PageResult<SysAcl> result = sysAclService.getPageByAclModuleId(aclModuleId, pageQuery);
        return JsonData.success(result);
    }

    /**
     * 获取用户权限点
     */
    @RequestMapping("/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("aclId") int aclId) {
        //TODO:
        Map<String, Object> map = Maps.newHashMap();
        List<SysRole> roles = sysRoleService.getRoleListByAclId(aclId);
        map.put("roles", roles);
        //通过角色列表得到用户列表
        List<SysUser> users = sysRoleService.getUserListByRoleList(roles);
        map.put("users", users);
        return JsonData.success(map);
    }

}
